package com.yunji.framework_template.web.config;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;

@Configuration
public class CuratorFrameworkConfig {
	@Value("#{settings['zookeeper.serverlist']}")
	private String serverlist;
	@Value("#{settings['zookeeper.sessionTimeout']}")
	private int sessionTimeout;
	@Value("#{settings['zookeeper.connectionTimeout']}")
	private int connectionTimeout;
	@Value("#{settings['zookeeper.retryTimes']}")
	private int retryTimes;
	@Value("#{settings['zookeeper.retryInterval']}")
	private int retryInterval;

	@Bean
	public CuratorFramework curatorFramework() {
		RetryNTimes retryPolicy = new RetryNTimes(retryTimes,retryInterval);
		CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(serverlist, sessionTimeout, connectionTimeout, retryPolicy);
		curatorFramework.start();
		return curatorFramework;
	}
}
